package dshop.action.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.ModelKit;
import appbase.jfinal.ext.RecordKit;
import appbase.utils.JsonUtil;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Area;
import dshop.model.company.Company;
import dshop.model.job.Job;
import dshop.model.member.MemberGeneralInfo;
import dshop.model.procurement.IndustryCategory;

/**
 * 招聘信息管理
 */
@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class JobAction extends ApiBaseController {

	public void list() {
		String keyWord = getPara("keyWord", "").trim();
		String status = getPara("checkStatus", "").trim();

		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];

		String select = " select id, account_id, company_name, address, "
				+ " job_name, job_description, job_number, reward, " + " start_date_time, end_date_time, "
				+ " contact_name, contact_phone, contact_email, "
				+ " check_status, check_date_time, add_date_time,is_delete ";
		String where = " from bbs_job where 1=1 ";
		String orderBy = " ";

		List<Object> params = new ArrayList<Object>();
		if (!status.isEmpty()) {
			if (status.equals("END")) {
				where += " and end_date_time <= now() ";
				where += " and is_delete = 'NO' ";
			} else if (status.equals("DELETE")) {
				where += " and is_delete = 'YES' ";
			} else {
				where += " and check_status = ? ";
				where += " and is_delete = 'NO' ";
				params.add(status);
			}
		}
		if (!keyWord.isEmpty()) {
			where += " and ( company_name like ? or contact_phone like ? or job_name like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (!columnOrder.isEmpty()) {
			orderBy += "order by " + columnProp + " " + columnOrder;
		} else {
			orderBy = " order by add_date_time desc";
		}

		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();

		Date now = new Date();
		for (Record r : list) {
			Date endDate = r.getDate("end_date_time");
			if (endDate != null && now.after(endDate)) {
				r.set("is_time_out", AppConst.STATUS_VAL_YES);
			} else {
				r.set("is_time_out", AppConst.STATUS_VAL_NO);
			}
		}

		RecordKit.putDateStr(list, "add_date_time", "add_date_time_str", "yyyy-MM-dd HH:mm");
		RecordKit.putDateStr(list, "check_date_time", "check_date_time_str", "yyyy-MM-dd HH:mm");
		RecordKit.putDateStr(list, "start_date_time", "start_date_time_str", "yyyy-MM-dd HH:mm");
		RecordKit.putDateStr(list, "end_date_time", "end_date_time_str", "yyyy-MM-dd HH:mm");

		renderSuccess();
	}

	public void detail() {
		int jobId = getParaToInt("id");

		Job job = Job.dao.findById(jobId);
		setAttr("job", job);
		IndustryCategory industryCategory = IndustryCategory.dao.findById(job.getInt("industry_id"));
		job.put("product_industry_name", industryCategory.getStr("name"));

		Integer accountId = job.getInt("account_id");
		job.put("account", MemberGeneralInfo.dao.findById(accountId));
		job.put("company", Company.getCompanyInfoByAccountId(accountId));

		Area.setNameToM(job, "province_code, city_code, area_code", "province, city, area");
		Date now = new Date();
		Date endDate = job.getDate("end_date_time");
		if (endDate != null && now.after(endDate)) {
			job.put("is_time_out", AppConst.STATUS_VAL_YES);
		} else {
			job.put("is_time_out", AppConst.STATUS_VAL_NO);
		}

		ModelKit.putDateFormat(job, "start_date_time", "start_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(job, "end_date_time", "end_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(job, "check_date_time", "check_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(job, "add_date_time", "add_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(job, "update_date_time", "update_date_time", "yyyy-MM-dd HH:mm");

		job.put("coverImgList", JSON.parseArray(StringUtils.defaultString(job.getStr("cover"), "[]"), String.class));
		job.put("environmentImage", JSON.parseArray(StringUtils.defaultString(job.getStr("environment_image"), "[]"), String.class));

		renderSuccess();
	}

	public void delete() {
		int jobId = getParaToInt("id");

		Job.delete(jobId);

		renderSuccess("删除成功");
	}

	@Before(Tx.class)
	public void end() {
		int jobId = getParaToInt("id");

		Job.setEnd(jobId);

		renderSuccess("结束成功");
	}

	public void checkStatusPass() {
		int jobId = getParaToInt("id");
		String remark = getPara("remark", "").trim();
		String checkStatus = AppConst.CHECK_STATUS_PASS;

		Job.setCheckStatus(jobId, checkStatus, remark);

		renderSuccess("成功设置为已通过");
	}

	public void checkStatusFail() {
		int jobId = getParaToInt("id");
		String remark = getPara("remark", "").trim();
		String checkStatus = AppConst.CHECK_STATUS_FAIL;

		Job.setCheckStatus(jobId, checkStatus, remark);

		renderSuccess("已设置为不通过");
	}

}
